package org.stu.tp.testCases.contact;

import org.stu.tp.core.BaseSac;
import org.stu.tp.core.DefaultAjaxSelenium;
import org.stu.tp.util.GetProperties;

public class ContactBaseSac extends BaseSac {
	/**
	 * Click the Telephone book at the left panel
	 * 
	 * @param selenium
	 */
	public void clickAccessContact(DefaultAjaxSelenium selenium) {
		selenium.clickAt("wb_0_Item_1", "");
		this.waitForPageToLoad(selenium);
	}

	/**
	 * Select one team by the name.
	 * @param selenium
	 * @param teamName
	 */
	public void selectTeam(DefaultAjaxSelenium selenium, String teamName){
		String path=this.getTeamPath(selenium, teamName);
		selenium.click(path);
		this.waitForPageToLoad(selenium);
	}
	
	/**
	 * select the team
	 * @param selenium
	 */
	public void selectTeamMyself(DefaultAjaxSelenium selenium){
		selenium.click("//tr[@id='ctl00_content_groupMenun0']//a");
		this.waitForPageToLoad(selenium);
	}

	/**
	 * click the save button the save the contact!
	 * 
	 * @param seleniumx
	 */
	public void clickSaveButton(DefaultAjaxSelenium selenium) {
		selenium.click("ctl00_content_btnEdit");
	}

	/**
	 * click the cancel button the cancel the save!
	 * 
	 * @param selenium
	 */
	public void clickCancelButton(DefaultAjaxSelenium selenium) {
		selenium.click("ctl00_content_btnDel");
	}

	/**
	 * Click the edit button after selecting one contact.
	 * 
	 * @param selenium
	 */
	public void clickEditButton(DefaultAjaxSelenium selenium) {
		selenium.click("ctl00_content_btnEdit");
		this.waitForPageToLoad(selenium);
	}

	public void clickDeleteButton(DefaultAjaxSelenium selenium) {
		selenium.click("ctl00_content_btnDel");
	}
	
	/**
	 * Get the contact number in the team.
	 * 
	 * @param selenium
	 * @param teamIndex
	 * @return
	 */
	public String getContactNumOfTeam(DefaultAjaxSelenium selenium, String teamName) {
		String num;
		String str = "";
		String path=this.getTeamPath(selenium, teamName);
		
		str = selenium.getText(path);
		num = str.split("\\s+")[1];
		return num;
	}
	
	/**
	 * Get number of all contacts in the current account.
	 * @param selenium
	 * @return
	 */
	public String getContactNumOfAllContact(DefaultAjaxSelenium selenium){
		String num;
		String str = "";
		str = selenium.getText("ctl00_content_lbList");
		num = str.split("\\s+")[1];
		return num;
	}

	/**
	 * Get the name from the team identity.
	 * 
	 * @param selenium
	 * @param teamIndex
	 * @return
	 */
	public String getTeamName(DefaultAjaxSelenium selenium, String teamIndex) {
		String num;
		String str = "";
		str = selenium.getText("//tr[@id='ctl00_content_groupMenun" + teamIndex
				+ "']//a");
		num = str.split("\\s+")[0];
		return num;
	}

	/**
	 * Get the X-path value by the team name.
	 * @param selenium
	 * @param teamName
	 * @return
	 */
	public String getTeamPath(DefaultAjaxSelenium selenium, String teamName) {
		String path = "";
		boolean flag = true;
		Integer i = 1;
		
		while (flag) {
			String value = "";
			path = "//tr[@id='ctl00_content_groupMenun" + i.toString()
			+ "']//a";
			
			if(!selenium.isElementPresent(path)){
				path=null;
				break;
			}else {
				value = this.getTeamName(selenium, i.toString());
				if (value.equals(teamName)) {
					break;
				} 
			}
			i++;
		}
		return path;
	}
	
	/**
	 * Login with the specify account and access to contact book page.
	 * @param selenium
	 * @param userName
	 * @param password
	 */
	public void loginAndAccessContactBook(DefaultAjaxSelenium selenium,String userName,String password){
		this.login(selenium, userName, password);
		this.clickAccessContact(selenium);
	}
	
	/**
	 * Login with the default account and access to contact book page.
	 * @param selenium
	 */
	public void loginAndAccessContactBook(DefaultAjaxSelenium selenium){
		loginAndAccessContactBook(selenium,GetProperties.getUserName(), GetProperties.getPassword());
	}
}
